//
//  OperationBillGoodListVC.m
//  LogisticsSupplier
//
//  Created by 吕晶 on 2020/11/10.
//  Copyright © 2020 吕晶. All rights reserved.
//

#import "OperationBillGoodListVC.h"
#import "OperationBillGoodModel.h"
#import "OperationBillGoodListHeadView.h"
#import "OperationBillGoodListCell.h"

@interface OperationBillGoodListVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIView *bottomView;
@property (nonatomic, strong) UILabel *weightLab;
@property (nonatomic, strong) UILabel *volumeLab;
@property (nonatomic, strong) UILabel *pieceLab;
@property (nonatomic, strong) UILabel *countLab;
@property (nonatomic, strong) NSArray <OperationBillGoodModel *>*array;

@end

@implementation OperationBillGoodListVC

-(NSArray<OperationBillGoodModel *> *)array {
    if (!_array) {
        _array = [NSArray array];
    }
    return _array;
}

-(UIView *)bottomView {
    if (!_bottomView) {
        _bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenH - BottomHeight - NaviHeight - 100, ScreenW, 100 + BottomHeight)];
        _bottomView.backgroundColor = HexColor(0xffffff);
        UILabel *titleLab = [[UILabel alloc] initWithFrame:CGRectMake(15, 15, ScreenW - 30, 15)];
        titleLab.text = @"运输计划总计";
        titleLab.font = BoldFontOfSize(14);
        titleLab.textColor = HexColor(0x2B2C44);
        [_bottomView addSubview:titleLab];
        
        self.weightLab = [[UILabel alloc] initWithFrame:CGRectMake(15, (100 - 35 - 15 - 15) * 0.5 + titleLab.nim_bottom, (ScreenW - 30) * 0.5, 15)];
        self.weightLab.textColor = HexColor(0x9C9DB3);
        self.weightLab.font = BoldFontOfSize(14);
        [_bottomView addSubview:self.weightLab];
        
        self.volumeLab = [[UILabel alloc] initWithFrame:CGRectMake(self.weightLab.nim_right, self.weightLab.nim_top, self.weightLab.nim_width, self.weightLab.nim_height)];
        self.volumeLab.textColor = HexColor(0x9C9DB3);
        self.volumeLab.font = BoldFontOfSize(14);
        [_bottomView addSubview:self.volumeLab];
        
        self.pieceLab = [[UILabel alloc] initWithFrame:CGRectMake(self.weightLab.nim_left, self.weightLab.nim_bottom + 5, self.weightLab.nim_width, self.weightLab.nim_height)];
        self.pieceLab.textColor = HexColor(0x9C9DB3);
        self.pieceLab.font = BoldFontOfSize(14);
        [_bottomView addSubview:self.pieceLab];
        
        self.countLab = [[UILabel alloc] initWithFrame:CGRectMake(self.pieceLab.nim_right, self.pieceLab.nim_top, self.weightLab.nim_width, self.weightLab.nim_height)];
        self.countLab.textColor = HexColor(0x9C9DB3);
        self.countLab.font = BoldFontOfSize(14);
        [_bottomView addSubview:self.countLab];
    }
    return _bottomView;
}

-(UIScrollView *)scrollView {
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH - NaviHeight - self.bottomView.nim_height)];
        _scrollView.bounces = NO;
    }
    return _scrollView;
}

-(UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, self.scrollView.nim_height)];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = Color_backgroundColor;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        __weak_self;
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [weakself requestForData];
        }];
        _tableView.ly_emptyView = [LYEmptyView emptyViewWithImageStr:@"img_occupy"
             titleStr:@""
            detailStr:@""];
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"货物明细清单";
    [self.view addSubview:self.bottomView];
    [self.view addSubview:self.scrollView];
    [self.scrollView addSubview:self.tableView];
    self.scrollView.contentSize = CGSizeMake((ScreenW * 0.25) * 11, self.scrollView.bounds.size.height);
    self.tableView.nim_width = self.scrollView.contentSize.width;
    [self requestForData];
}

- (void)requestForData {
    [HttpRequest PostWithApi:LogisticsSupplierOperationBillGoodList params:@{@"o_id":@(self.o_id)} progress:^(NSProgress * _Nonnull progress) {
            
    } success:^(NSURLSessionDataTask * _Nonnull operation, id  _Nonnull responseObject, BOOL code, NSInteger errorCode) {
        if (code) {
            self.array = [OperationBillGoodModel mj_objectArrayWithKeyValuesArray:[responseObject objectForKey:@"good_list"]];
            [self.tableView reloadData];
            self.weightLab.text = [NSString stringWithFormat:@"重量：%@kg",[[responseObject objectForKey:@"total_info"] objectForKey:@"total_weight"]];
            self.volumeLab.text = [NSString stringWithFormat:@"体积：%@m³",[[responseObject objectForKey:@"total_info"] objectForKey:@"total_volume"]];
            self.pieceLab.text = [NSString stringWithFormat:@"件数：%@",[[responseObject objectForKey:@"total_info"] objectForKey:@"goods_jnum"]];
            self.countLab.text = [NSString stringWithFormat:@"货物数量：%@",[[responseObject objectForKey:@"total_info"] objectForKey:@"goods_num"]];
        }
        [self.tableView.mj_header endRefreshing];
    } fail:^(NSError * _Nonnull error) {
        [self.tableView.mj_header endRefreshing];
    }];
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    OperationBillGoodListHeadView *view = [OperationBillGoodListHeadView new];
    return view;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 40;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.array.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    OperationBillGoodListCell *cell = [OperationBillGoodListCell OperationBillGoodListCellWithTableView:tableView];
    OperationBillGoodModel *model = self.array[indexPath.row];
    cell.model = model;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    OperationBillGoodListCell *cell = [OperationBillGoodListCell OperationBillGoodListCellWithTableView:tableView];
    OperationBillGoodModel *model = self.array[indexPath.row];
    cell.model = model;
    return cell.height;
}

@end
